<?php

/**
 * @Author:      lyt
 * @DateTime:    2016-07-03 12:38:50
 * @Description: 用于生成 每个门店的  订单的期末库存   
 * 逻辑 是    每天的期末库存 加上 没有结算订单的库存 就是 当前库存
 */



$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 


$input_1 = array(
    'item_type'     => 0,                     
    'item_id'       => 0,     
    'shop_id'       => 0,  
    'date'          => date("Y-m-d",time()-86400),
    'data_id'       => NULL,
    'number'        => NULL,
    'amount'        => NULL,
    'created_at'    => date("Y-m-d H:i:s"),
    'updated_at'    => date("Y-m-d H:i:s"),
);

MaintenanceWarehouseOrderStock::where("date",date("Y-m-d",time()-86400))->delete();

$mdb=MainDateBase::where("status",1)->get();
foreach ($mdb as $_mdb) {//暂时锁库 不出入库以免影响统计 暂时 不做 可能统计结果出现差别

}


foreach ($mdb as $_mdb) {//对每个库 的耗材进行盘点
    //查出当前暂时  没有结算的订单
    $order_temp = Orders::whereIn('status',array(1215,1216,1416,1415,1420))
                ->where('shop_id',$_mdb->shop_id)
                ->where('place',2)
                ->select(array('order_id'))
                ->get();
    $order_array=array(0);
    foreach ($order_temp as $_o) {
        $order_array[]=$_o->order_id;
    }
    $order_temp1 = Orders::whereIn('status',array(1411))
                ->where('shop_id',$_mdb->shop_id)
                ->where('place',2)
                ->where('order_type',2)
                ->select(array('order_id'))
                ->get();
    foreach ($order_temp1 as $_o) {
        $order_array[]=$_o->order_id;
    }
    //吧算在第二天结算的也去除
    $order_temp2 = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->where('pay_time',">",date("Y-m-d",time()-86400).' 23:59:59')
            ->where('shop_id',$_mdb->shop_id)
            ->where('place',2)
            ->select(array('order_id'))
            ->get();
    foreach ($order_temp2 as $_o) {
        $order_array[]=$_o->order_id;
    }

    $ckwjs=DB::select("SELECT
                moi.item_type,
                moi.item_id,
                sum(moi.number-moi.return_numer) number,
                sum(
                    (moi.number-moi.return_numer) * moi.tax_purchase_price
                ) amount
            FROM
                maintenance_out_item moi
            LEFT JOIN maintenance_out_order moo ON moo.id = moi.order_out_id
            WHERE
                moi.shop_id =?
            AND moi. STATUS = 1
            AND moi.data_id =?
            and moo.order_id in(".implode(",",$order_array).") 
            GROUP BY
                moi.item_type,
                moi.item_id",array($_mdb->shop_id,$_mdb->id));
    $temp_arr=array();
    foreach ($ckwjs as $_sin_item) {
        $temp_arr[$_sin_item->item_type.'-'.$_sin_item->item_id]=$_sin_item;
    }

    $bag = array();
    $sql="select item_type,item_id,sum(current_number) number,sum(current_number*tax_purchase_price) amount from maintenance_storage_item msi
            where msi.shop_id=? and msi.status=1 and msi.type=1 and msi.data_id=?  group by msi.item_type,msi.item_id";
    $s_item=DB::select($sql,array($_mdb->shop_id,$_mdb->id));
    foreach ($s_item as $_item) {
        $temp_arry=isset($temp_arr[$_item->item_type.'-'.$_item->item_id])?$temp_arr[$_item->item_type.'-'.$_item->item_id]:array();

        $input_1['item_type'] = $_item->item_type;
        $input_1['item_id'] = $_item->item_id;
        $input_1['shop_id'] = $_mdb->shop_id;
        $input_1['data_id'] = $_mdb->id;
        $input_1['number'] = isset($temp_arry->number)?($_item->number+$temp_arry->number):$_item->number;
        $input_1['amount'] = isset($temp_arry->amount)?($_item->amount+$temp_arry->amount):$_item->amount;
        $bag[] = $input_1;          
        if(sizeof($bag)>2000){
            DB::table('maintenance_warehouse_order_stock')->insert($bag);    
            unset($bag);
            $bag=array();     
        } 
    }
    if(sizeof($bag)>0){
        DB::table('maintenance_warehouse_order_stock')->insert($bag);       
    }    
    Log::info($_mdb->name." 订单库耗材盘点完毕"); 
    echo $_mdb->name." 订单库耗材盘点完毕";
}

